﻿#include <QDebug>
#include <QMutex>
#include "DatabaseMgmt.h"

QMutex g_databaseMutex;

CDatabaseMgmt* CDatabaseMgmt::getInstance()
{
	static CDatabaseMgmt* pMgmtInst = nullptr;

	g_databaseMutex.lock();
	if (pMgmtInst == nullptr)
	{
		pMgmtInst = new CDatabaseMgmt();
	}
	g_databaseMutex.unlock();

	return pMgmtInst;
}

CDatabaseMgmt::CDatabaseMgmt()
{
	init();
}

CDatabaseMgmt::~CDatabaseMgmt()
{

}

void CDatabaseMgmt::execSql(QString strSql, QSqlQuery & query)
{
	if (strSql.isEmpty())
	{
		return;
	}
	query = m_sqliteDatabase.exec(strSql);
}

void CDatabaseMgmt::init()
{
	m_sqliteDatabase = QSqlDatabase::addDatabase("QSQLITE");
	// 设置db文件名称
	m_sqliteDatabase.setDatabaseName("../data/detection.db3");
// 	// 设置登陆账号
// 	m_sqliteDatabase.setUserName("100001");
// 	// 设置登陆密码
// 	m_sqliteDatabase.setPassword("123456");

	bool ret = m_sqliteDatabase.open();

	qInfo() << "sqlite open state:" << ret;
}
